<?php
/* wwwschool project

Copyright (C) 2009  Phillip Aldridge
Email : info@imi21.com
Web site http://www.imi21.com

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
*/


/**
 *  File : move.php
 *  called only via ajax 
 *  
 *  requires POST  f, id
 *  the div element is 't+id' 
 *  
 *  move in the table $f, the $id $position up or down
 */  

//$$TODO Who is allowed to edit this
$message="";
switch ($position)
{
  case 'up': $direction=-1; break;
  case 'down': $direction=1; break;
  default:exit;break;
}


$db=new WS3_Database();
$id=$db->makeSafeInt($id);
$f=$db->makeSafeString($f);
$table=strtolower($f);
switch ($table)
{
  case 'content':$parent_id='module_id';

                break;
  case 'lesson2course':$parent_id='course_id';
                break;
                
  case 'module2lesson':$parent_id='lesson_id';
                break;                
}


// get the content which we want to move
$q="SELECT `position`, `$parent_id` FROM `$table` WHERE 1 AND `id`= '$id' LIMIT 1";


if ($db->query($q) && $db->nextRecord() )
{
  $p_id = $db->f($parent_id);
  $position1 = $db->f('position')+$direction;
  $position2 = $db->f('position');//+$direction;
  
  // get the content which will have to move
  $db2=new WS3_Database();
  $q="SELECT `id` FROM `$table` WHERE 1
    AND `$parent_id` = '$p_id'
    AND `position` = '$position1' LIMIT 1";
    
  
  if ($db2->query($q) && $db2->nextRecord() )
  {
    $db3=new WS3_Database();
    
    // update new position
    $q="UPDATE `$table` SET `position` = '$position1'
    WHERE 1
    AND `id` = '$id'
    LIMIT 1";
    $db3->query($q);
    
    // update new position on other record
    $q="UPDATE `$table` SET `position` = '$position2'
    WHERE 1
    AND `id` = '".$db2->f('id')."'
    LIMIT 1";
    $db3->query($q);
    
    $message="Moved";
  }else
  {
    $message="Unable to move";
  } 
}else
  {
    $message="Error id not found";
  }


switch ($table)
{  //$$TODO a bit of mess here post and ids, needs to be harmoniozed
  case 'content':$_POST['id']=$p_id;
                include 'listContents.php';
                break;
  case 'lesson2course':$_POST['id']=$p_id;
                include 'listLessons.php';
                break;
  case 'module2lesson':$id=$p_id;
                include 'listLessonModules.php';
                break;                
}
echo "document.getElementById('messages').innerHTML='$message';";
?>
